package com.toilet.easyvideo.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.toilet.easyvideo.config.JWTConfig;
import com.toilet.easyvideo.domain.SysUser;
import com.toilet.easyvideo.exception.ServiceException;
import com.toilet.easyvideo.request.NormalRegisterRequest;
import com.toilet.easyvideo.response.RegisterResponse;
import com.toilet.easyvideo.security.LoginUser;
import com.toilet.easyvideo.constant.JWTConstant;
import com.toilet.easyvideo.request.LoginRequest;
import com.toilet.easyvideo.response.LoginResponse;
import com.toilet.easyvideo.service.LoginService;
import com.toilet.easyvideo.service.SysUserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author snowfrake
 * @date 2023/11/3
 */
@Slf4j
@Service
public class LoginServiceImpl implements LoginService {

    @Resource
    private AuthenticationManager authenticationManager;
    @Resource
    private JWTConfig jwtConfig;
    @Resource
    private SysUserService sysUserService;
    @Resource
    private PasswordEncoder passwordEncoder;

    @Override
    public LoginResponse login(LoginRequest loginRequest) {
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(
                loginRequest.getUserName(), loginRequest.getPassword());
        Authentication authenticate = authenticationManager.authenticate(authenticationToken);
        if (ObjectUtil.isEmpty(authenticate)) {
            log.info("登录失败");
        }

        LoginUser loginUser = (LoginUser) authenticate.getPrincipal();
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put(JWTConstant.UNIQUE_KEY, loginUser.getUsername());
        String token = jwtConfig.createToken(hashMap);
        LoginResponse response = new LoginResponse();
        response.setToken(token);
        response.setTokenHead(jwtConfig.getHeader());
        return response;
    }

    @Override
    public RegisterResponse normalRegister(NormalRegisterRequest loginRequest) {
        SysUser sysUser = sysUserService.lambdaQuery().eq(SysUser::getUserName, loginRequest.getUserName()).one();
        if (ObjectUtil.isNotEmpty(sysUser)) {
            throw new ServiceException("用户已存在,请更换用户名进行注册");
        }
        if (Objects.equals(loginRequest.getPassword(), loginRequest.getConfirmPassword()) == Boolean.FALSE) {
            throw new ServiceException("前后密码不一致");
        }
        String encodePwd = passwordEncoder.encode(loginRequest.getPassword());
        SysUser register = new SysUser();
        register.setUserName(loginRequest.getUserName());
        register.setPassword(encodePwd);
        boolean save = sysUserService.save(register);
        RegisterResponse response = new RegisterResponse();
        response.setRegisterResult(save);
        return response;
    }
}
